package com.iamback.dy.examine.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.iamback.dy.entity.Resource;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.service.IResourceService;
import com.iamback.dy.examine.vo.ZtreeView;
import com.iamback.dy.mapper.ResourceMapper;
import com.iamback.dy.mapper.RoleMapper;
@Service
public class ResourceServiceImpl implements IResourceService {

	@Autowired
	private ResourceMapper resourceMapper;
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public Page<Resource> findResourcePage(String searchText,Integer pageNum,Integer numPerPage){
		Map<String, Object> map = new HashMap<String, Object>();
		int start = pageNum ==0?0:(pageNum-1)*numPerPage;
		map.put("name", searchText);
		map.put("pageNum", start);
		map.put("numPerPage", numPerPage);
		Page<Resource> page =new Page<Resource>();
		Integer count = resourceMapper.findResourceByPageCount(map);
		if(count!=null && count>0){
			List<Resource> resources = resourceMapper.findResourceByPage(map);
			page.setContent(resources);
			page.setTotalElements(count);
		}else{
			page.setTotalElements(0);
		}
		return page;
	}
	
	
	@Override
	public List<ZtreeView> tree(int roleId) {
		List<ZtreeView> resulTreeNodes = new ArrayList<ZtreeView>();
		Set<Resource> roleResources = getResourceSet(roleId);
		resulTreeNodes.add(new ZtreeView(0L, null, "系统菜单", true));
		ZtreeView node;
//		Sort sort = new Sort(Direction.ASC, "parent", "id", "sort");
		List<Resource> all = findResourceParent();
		for (Resource resource : all) {
			node = new ZtreeView();
			node.setId(Long.valueOf(resource.getId()));
			if (resource.getParent() == null) {
				node.setpId(0L);
			} else {
				node.setpId(Long.valueOf(resource.getParent().getId()));
			}
			node.setName(resource.getName());
			if (roleResources != null && roleResources.contains(resource)) {
				node.setChecked(true);
			}
			resulTreeNodes.add(node);
		}
		return resulTreeNodes;
	}
	@Override
	public List<Resource> findResourceParent(){
		List<Resource> all= resourceMapper.findResource(null);
		List<Resource> resources =new ArrayList<Resource>();
		for(Resource resource:all){
			Resource parent= resourceMapper.findResourceById(resource.getParentId());
			resource.setParent(parent);
			resources.add(resource);
		}
		return resources;
	}
	@Override
	public Resource getResourceBy(Integer id){
		Resource resource=resourceMapper.findResourceById(id);
		resource.setParent(resourceMapper.findResourceById(resource.getParentId()));
		return resource;
	}
	
	@Override
	public Set<Resource> getResourceSet(Integer roleId){
		List<Resource> list = resourceMapper.findResourceByRole(roleId);
		Set<Resource> permsSet = new HashSet<Resource>();
		for(Resource perm:list){
			permsSet.addAll(Arrays.asList(perm));
		}
		return permsSet;
	}
	
	
	@Override
	public void saveOrUpdate(Resource resource) {
		if(resource.getId() != null){
			Resource dbResource = resourceMapper.findResourceById(resource.getId());
			dbResource.setUpdateTime(new Date());
			dbResource.setName(resource.getName());
			dbResource.setSourceKey(resource.getSourceKey());
			dbResource.setType(resource.getType());
			dbResource.setSourceUrl(resource.getSourceUrl());
			dbResource.setLevel(resource.getLevel());
			dbResource.setSort(resource.getSort());
			dbResource.setIsHide(resource.getIsHide());
			dbResource.setIcon(resource.getIcon());
			dbResource.setDescription(resource.getDescription());
			dbResource.setUpdateTime(new Date());
			dbResource.setParent(resource.getParent());
			resourceMapper.updateResource(dbResource);
		}else{
			resource.setCreateTime(new Date());
			resource.setUpdateTime(new Date());
			resourceMapper.saveResource(resource);
		}
	}
	@Override
	@CacheEvict(value = "resourceCache")
	public void delete(Integer id) {
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("resourceId", id);
		roleMapper.deleteResourceRole(map);
		resourceMapper.deleteResourceById(id);
	}
}
